package com.lt.contract.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lt.contract.bean.utils.ExcelImport;
import com.lt.contract.dao.InnovativeBusinessDao;
import com.lt.contract.entity.InnovativeBusiness;
import com.lt.contract.service.InnovativeBusinessService;
import com.lt.report.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * (InnovativeBusiness)表服务实现类
 *
 * @author makejava
 * @since 2020-02-25 11:36:19
 */
@Service("innovativeBusinessService")
@Slf4j
public class InnovativeBusinessServiceImpl implements InnovativeBusinessService {
    @Resource
    private InnovativeBusinessDao innovativeBusinessDao;

    @Autowired
    AsyncService asyncService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public InnovativeBusiness queryById(String id) {
        return this.innovativeBusinessDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<InnovativeBusiness> queryAllByLimit(int offset, int limit) {
        return this.innovativeBusinessDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<InnovativeBusiness> queryAll(Map<String, Object> map) {
        return innovativeBusinessDao.queryAll(map);
    }

    @Override
    public Long queryCount(Map<String, Object> map) {
        return innovativeBusinessDao.queryCount(map);
    }

    @Override
    public int insertAll(List<InnovativeBusiness> list) throws Exception {
        return innovativeBusinessDao.insertAll(list);
    }

    @Override
    public List<InnovativeBusiness> exportList(Map<String, Object> map) {
        return innovativeBusinessDao.exportList(map);
    }

    /**
     * 新增数据
     *
     * @param innovativeBusiness 实例对象
     * @return 实例对象
     */
    @Override
    public InnovativeBusiness insert(InnovativeBusiness innovativeBusiness) {
        this.innovativeBusinessDao.insert(innovativeBusiness);
        return innovativeBusiness;
    }

    /**
     * 修改数据
     *
     * @param innovativeBusiness 实例对象
     * @return 实例对象
     */
    @Override
    public InnovativeBusiness update(InnovativeBusiness innovativeBusiness) {
        this.innovativeBusinessDao.update(innovativeBusiness);
        return this.queryById(innovativeBusiness.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.innovativeBusinessDao.deleteById(id) > 0;
    }

    @Override
    public String getBankListByExcel(InputStream in, String fileName, String StaffId) throws Exception {
        //选择文件中的列
        String tableName = "";
        if (fileName.contains("创新业务")) {
            tableName = "innovativeBusiness";
        } else {
            log.error("上传文件不符合规定");
            return "请上次相关文件";
        }
        //创建Excel工作薄
        Workbook work = ExcelImport.getWorkbook(in, fileName);
        Sheet sheet = work.getSheetAt(0);//只取第一个Sheet
        List<String> isRecur = ExcelImport.isRecur(sheet,6);
        if(isRecur.size()>0){
            JSONObject object = new JSONObject();
            object.put("您上传的合同号有重复，请确认！",isRecur);
            return JSON.toJSONString(object);
        }
        List<Integer> column = Arrays.asList(1,2,3,4,5,6,7,10,11,12,13,14);
        String empty = ExcelImport.isEmpty(sheet, column);
        if(!empty.equals("true")){
            return empty;
        }
        List<List<String>> list = ExcelImport.queryRepetition(sheet, 6);//第6列是计费号码
        String cycleIdNew = ExcelImport.getCellValue(sheet.getRow(2).getCell(13));//获取上传文件中的账期
        List<Map<String, Object>> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            list1.addAll(innovativeBusinessDao.queryRepetition(list.get(i),cycleIdNew));
        }
        if (list1.size() > 0) {
            return JSON.toJSONString(list1);
        }
        List<List<InnovativeBusiness>> lists = listInnovativeBusiness(sheet, StaffId);
        int size = lists.size();
        for (int i = 0; i < size; i++) {
            List<InnovativeBusiness> list2 = lists.get(i);
            asyncService.executeInnovative(list2);
        }
        return "上传成功";
    }

    /**
     * 导入物联网业务
     *
     * @param sheets 导入物联网业务
     * @return
     */
    public List<List<InnovativeBusiness>> listInnovativeBusiness(Sheet sheets, String StaffId) throws Exception {
        List<List<InnovativeBusiness>> lists = new ArrayList<>();
        List<InnovativeBusiness> list = new ArrayList<>();
        Cell cell = null;
        for (int j = sheets.getFirstRowNum() + 1; j <= sheets.getLastRowNum() + 1; j++) {
            Row row = sheets.getRow(j);
            int flag = 200;
            if (list.size() != 0 && list.size() % flag == 0 || j == sheets.getLastRowNum() + 1) {
                List<InnovativeBusiness> copy = new ArrayList<>(list);
                lists.add(copy);
                list.clear();
            }
            if (row == null || row.getFirstCellNum() == j || row.getCell(1) == null) {
                continue;
            } else if (ExcelImport.isAllRowEmpty(row, row)) {
                continue;
            }
            InnovativeBusiness innovativeBusiness = new InnovativeBusiness();
            for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                cell = row.getCell(y);
                if (cell == null || cell.toString().equals("")) {
                    continue;
                }
                switch (y) {
                    case 1:
                        innovativeBusiness.setAreaCode(ExcelImport.getCellValue(cell));
                        break;
                    case 2:
                        innovativeBusiness.setTrellisCode(ExcelImport.getCellValue(cell));
                        break;
                    case 3:
                        innovativeBusiness.setTrellisName(ExcelImport.getCellValue(cell));
                        break;
                    case 4:
                        innovativeBusiness.setYzStaffId(ExcelImport.getCellValue(cell));
                        break;
                    case 5:
                        innovativeBusiness.setAccountManager(ExcelImport.getCellValue(cell));
                        break;
                    case 6:
                        innovativeBusiness.setContractNoGlobal(ExcelImport.getCellValue(cell));
                        break;
                    case 7:
                        innovativeBusiness.setContractName(ExcelImport.getCellValue(cell));
                        break;
                    case 8:
                        innovativeBusiness.setCompanyName(ExcelImport.getCellValue(cell));
                        break;
                    case 9:
                        innovativeBusiness.setTotalAmount(ExcelImport.getCellValue(cell));
                        break;
                    case 10:
                        innovativeBusiness.setRepositoryNameVendor(ExcelImport.getCellValue(cell));
                        break;
                    case 11:
                        innovativeBusiness.setBusinessCodingCode(ExcelImport.getCellValue(cell));
                        break;
                    case 12:
                        innovativeBusiness.setBusinessCodingName(ExcelImport.getCellValue(cell));
                        break;
                    case 13:
                        innovativeBusiness.setCycleId(ExcelImport.getCellValue(cell));
                        break;
                    case 14:
                        innovativeBusiness.setAmount(ExcelImport.getCellValue(cell));
                        break;
                }
            }
            innovativeBusiness.setInputStaffId(StaffId);
            innovativeBusiness.setUpdateStaffid(StaffId);
            list.add(innovativeBusiness);

        }
        return lists;
    }
}
